<template>
  <div>
    <!-- 面包屑 ------------------------------->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 --------------------------------->
    <el-card>
      <!-- 栅格布局 -->
      <el-row :gutter="20">
        <!-- 第一列 -->
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            @clear="getOrderList"
            v-model="queryData.query"
            @change="getOrderList"
            clearable
          >
            <!-- 搜过按钮 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column label="#" type="index" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="{ row }">
            <el-tag type="success" v-if="row.order_pay === '1'">已付款</el-tag>
            <el-tag type="warning" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="{ row }">
            {{ row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showAddressDialog"
            ></el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location-outline"
              @click="showProgressDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="pageSizeChange"
        @current-change="pageChange"
        :current-page="queryData.pagenum"
        :page-sizes="[2, 3, 5, 10]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressDialogVisible"
      width="50%"
      @close="handleAddressDialogClose"
    >
      <!-- 新增表单 -->
      <el-form
        ref="addressFormRef"
        :model="addressForm"
        :rules="addressRules"
        label-width="80px"
      >
        <el-form-item label="省市区" prop="address1">
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="详情地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 地址信息对话框 -->
    <el-dialog title="提示" :visible.sync="progressDialogVisible" width="50%">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data () {
    return {
      queryData: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 2 // 页容量
      },
      orderList: [], // 用户数组
      total: 0, // 总用户数
      addressDialogVisible: false, // 是否显示新增对话框
      addressForm: { // 表单数据对象
        address1: [], // 省市区
        address2: '' // 详情地址
      },
      addressRules: { // 表单校验规则对象
        address1: [
          { required: true, message: '请选择省市区', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请选择详情地址', trigger: 'blur' }
        ]
      },
      cityData,
      progressDialogVisible: false,
      progressInfo: {}
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    async showProgressDialog () {
      // const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      const res = {
        data: [
          {
            time: '2018-05-10 09:39:00',
            ftime: '2018-05-10 09:39:00',
            context: '已签收,感谢使用顺丰,期待再次为您服务',
            location: ''
          },
          {
            time: '2018-05-10 08:23:00',
            ftime: '2018-05-10 08:23:00',
            context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
            location: ''
          },
          {
            time: '2018-05-10 07:32:00',
            ftime: '2018-05-10 07:32:00',
            context: '快件到达 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-10 02:03:00',
            ftime: '2018-05-10 02:03:00',
            context: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-09 23:05:00',
            ftime: '2018-05-09 23:05:00',
            context: '快件到达 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 21:21:00',
            ftime: '2018-05-09 21:21:00',
            context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 13:07:00',
            ftime: '2018-05-09 13:07:00',
            context: '顺丰速运 已收取快件',
            location: ''
          },
          {
            time: '2018-05-09 12:25:03',
            ftime: '2018-05-09 12:25:03',
            context: '卖家发货',
            location: ''
          },
          {
            time: '2018-05-09 12:22:24',
            ftime: '2018-05-09 12:22:24',
            context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
            location: ''
          },
          {
            time: '2018-05-08 21:36:04',
            ftime: '2018-05-08 21:36:04',
            context: '商品已经下单',
            location: ''
          }
        ],
        meta: { status: 200, message: '获取物流信息成功！' }
      }
      this.progressInfo = res.data
      this.progressDialogVisible = true
    },
    showAddressDialog () {
      this.addressDialogVisible = true
    },
    handleAddressDialogClose () { // 关闭对话框事件
      this.$refs.addressFormRef.resetFields()
    },
    pageSizeChange (newPageSize) {
    // 将 选择 的 新的 页容量 ，设置给 data
      this.queryData.pagesize = newPageSize
      // 重新请求 当前页数据
      this.getOrderList()
    },
    pageChange (newPageIndex) {
    // 将 被点击的 页码 设置给 data
      this.queryData.pagenum = newPageIndex
      // 重新请求 当前页数据
      this.getOrderList()
    },
    // 异步请求 用户列表
    async getOrderList () {
    // 发送异步请求，并 解构返回对象中的 data属性，赋值给 resBody
      const { data: resBody } = await this.$http.get('orders', {
        params: this.queryData
      })
      console.log(resBody)
      // 如果失败，则显示错误消息，并退出函数执行
      if (resBody.meta.status !== 200) return this.$msg.error(resBody.meta.msg)
      // 如果成功，则将返回的 分页数据 设置给 组件的 data，触发 视图的重绘
      this.orderList = resBody.data.goods
      // 将 返回的 总用户数量 设置给 data
      this.total = resBody.data.total
    }
  }
}
</script>

<style lang="less" scoped>
</style>
